var Constants = {
		MIN_WORD_LENGTH : 1,
		MAX_WORD_LENGTH : 255
}

function fieldLengthBetween (field, min_length, max_length) {
	if (min_length !== null && field.value.length < min_length) {
		return false;
	}
	if (max_length !== null && field.value.length > max_length) {
		return false;
	}
	return true;
}

function fieldIsInteger (field) {
	return (field.value != null && isInt(field.value));
}

// nebaigta funkcija nes nėra svarbi
// paskirtis - patikrinti ar tinkamas id 
// frazės ištrynimui
function dont_checkSelected() {
	var msgField = document.getElementById("deleteUpdateItem_msg");
	var selectedIdField = document.getElementById("selected_id");
	var optionsField = document.getElementById("");
	var messages = new Array();
	var valid = true;
	
	if (!fieldIsInteger(selectedIdField)) {
		valid = false;
		messages.push("netinka frazės id");
	}
}

function checkInsertWordForm() {
	var msgField = document.getElementById("insertWord_msg");
	var insertMotherField = document.getElementById("insert_mother");
	var insertForeField = document.getElementById("insert_fore");
	var messages = new Array();
	var valid = true;
	
	if (fieldIsTooShort(insertMotherField, MIN_WORD_LENGTH)) {
		valid = false;
		messages.push("kaip bus gimtąja kalba?");
		attachMsgHandler(insertMotherField, msgField);
	}
	
	if (fieldIsTooLong(insertMotherField, MAX_WORD_LENGTH)) {
		valid = false;
		messages.push("per ilga frazė gimtąja kalba?");
		attachMsgHandler(insertMotherField, msgField);
	}
	
	if (fieldIsTooShort(insertForeField, MIN_WORD_LENGTH)) {
		valid = false;
		messages.push("kaip bus užsienio kalba?");
		attachMsgHandler(insertForeField, msgField);
	}
	
	if (fieldIsTooLong(insertForeField, MAX_WORD_LENGTH)) {
		valid = false;
		messages.push("per ilga frazė užsienio kalba?");
		attachMsgHandler(insertForeField, msgField);
	}
	
	if (!valid) {
		msgField.innerHTML = messages.join("<br />");
	}
	
	return valid;
}

function checkDeleteWordForm() {
	var msgField = document.getElementById("deleteUpdateWord_msg");
	var foundWordsSelect = document.getElementById ("found_words_select");
	var selectedIdField = document.getElementById("selected_id");
	var messages = new Array();
	var valid = true;	
	
	if (fieldIsTooShort(selectedIdField, 1)) {
		valid = false;
		messages.push("reikia pasirinkti, kurį ištrinti");
		attachMsgHandler(foundWordsSelect, msgField);
	}
	
	if (!valid) {
		msgField.innerHTML = messages.join("<br />");
	}
	
	return valid;
}



function checkUpdateWordForm() {
	var msgField = document.getElementById = "deleteUpdateWord_msg";
	var updateMotherField = document.getElementById("update_mother");
	var updateForeField = document.getElementById("update_fore");
	var messages = new Array();
	var valid = true;	
	
	if (fieldIsTooShort(updateMotherField, MIN_WORD_LENGTH)) {
		valid = false;
		messages.push("kaip bus gimtąja kalba?");
		attachMsgHandler(updateMotherField, msgField);
	} 
	else if (fieldIsTooLong(updateMotherField, MAX_WORD_LENGTH)) {
		valid = false;
		messages.push("per ilga frazė gimtąja kalba?");
		attachMsgHandler(updateMotherField, msgField);
	}
	
	if (fieldIsTooShort(updateForeField, MIN_WORD_LENGTH)) {
		valid = false;
		messages.push("kaip bus užsienio kalba?");
		attachMsgHandler(updateForeField, msgField);
	}
	else if (fieldIsTooLong(updateForeField, MAX_WORD_LENGTH)) {
		valid = false;
		messages.push("per ilga frazė užsienio kalba?");
		attachMsgHandler(updateForeField, msgField);
	}
	
	if (!valid) {
		msgField.innerHTML = messages.join("<br />");
	}
	
	return valid;
}

function checkMultiword() {
	var MAX_RECORD_COUNT = 1000;
	var CHUNKS_DELIMITER = "|";
	var msgField = document.getElementById("uploadMulti_msg");
	var taValue = trim((document.getElementById("ta_multiword").value).replace(/(<([^>]+)>)/ig,""));
	var messages = new Array();
	var valid = true;
	var rows = taValue.split("\n", MAX_RECORD_COUNT);
	// alert("rows " + rows.length);
	var chunks;
	var first;
	var second;
	// kiekvieną eilutę kapoti į frazes, patikrinti ar visos kapojasi
	for (var i=0; i<rows.length; i++) {
		// alert ("loop");
		chunks = rows[i].split(CHUNKS_DELIMITER, 3);
		// alert("chunks count: " + chunks.length);
		if (chunks.length != 2) {
			messages.push("neteisingas " + (i+1) + "-os eilutės formatas.");
			valid = false;
		}
		// jeigu teisingai kapojasi į dvi frazes,
		// patikrinti, ar teisingi frazių ilgiai
		else {
			first = (trim(chunks[0])).length;
			second = (trim(chunks[1])).length;
			if (first < MIN_WORD_LENGTH || first > MAX_WORD_LENGTH) {
				messages.push("blogas " + (i+1) + "-os eilutės pirmosios dalies ilgis.");
				valid = false;
			}
			if (second < MIN_WORD_LENGTH || second > MAX_WORD_LENGTH) {
				messages.push("blogas " + (i+1) + "-os eilutės antrosios dalies ilgis.");
				valid = false;
			}
		}
	}
	
	// alert("messages " + messages.length);
	
	if (!valid) {
		msgField.innerHTML = messages.join("<br />");
	}	
	return valid;
}